// JavaScript Document
((function(a){
	var kqeditor = function(opts){
		//解析关于换行符的部分
		var analysisBr = function(code){
			code = code.replace(/\n/g,function(rs){
				return '<br />';
			});
			return code;
		}
		//关于语言模块的解析
		var analysisLanguage = function(code){
			code = code.replace(/\`{3}(\w{1,10})\n([\s\S]+?(\n\`{3}))/g,function(rs,$1,$2){
				$2 = $2.replace(/\`{3}$/,'');
				return '<pre><code class="language-'+$1+'">'+$2+'</code></pre>';
			});
			return code;
		}
		//关于H标签的解析
		var analysisHlabel = function(code){
			code = code.replace(/(\#{1,6})\s([\s\S]+?[\n])/g,function(rs,$1,$2){
				$2 = $2.replace(/\n/,'');
				return '<h'+$1.length+'>'+$2+'</h'+$1.length+'>';
			});
			code = code.replace(/(\#{1,6})\s([\s\S]+)$/g,function(rs,$1,$2){
				$2 = $2.replace(/\n/,'');
				return '<h'+$1.length+'>'+$2+'</h'+$1.length+'>';
			});
			return code;
		}
		//解析关于列表的信息
		var analysisList = function(code){
			code = code.replace(/\:\:list\n([\s\S]+?(\nend\slist))/g,function(rs,$1,$2){
				var arr = $1.replace($2,'').split("\n");
				var str = '';
				for(var i=0;i<arr.length;i++){
					str += '<li class="editor-text-listli"><span>'+arr[i]+'</span></li>';	
				}
				return '<pre><ul class="editor-text-listul">'+str+'</ul></pre>';
			});
			return code;
		}
		var analysisBase = function(code){
			code = code.replace(/\</g,function(rs){ return '&lt;';});
			code = code.replace(/\>/g,function(rs){ return '&gt;';});
			return code;
		}
		
		this.input = '';
		this.parse = function(input){
			this.input = input;
			this.input = analysisBase(this.input);//基础的替换
			this.input = analysisLanguage(this.input);//关于语言模块的解析
			this.input = analysisHlabel(this.input);//关于H标签的解析
			this.input = analysisList(this.input);//关于列表的解析
			this.input = analysisBr(this.input);//解析\n换行符
			return this.input;
		}
	};
	a.kuoqiJs.plugIn.register('Editor',kqeditor);
})(this))